/*
 * @Author: Tendo 
 * @Date: 2021-09-28 14:32:06 
 * @Last Modified by: Tendo
 * @Last Modified time: 2021-09-28 14:58:44
 */


/**
 * Initialize your data structure here.
 */
var MyQueue = function () {
    // 使用两个数组的栈方法（push, pop） 实现队列
    this.stack1 = []; // 输入栈
    this.stack2 = []; // 输出栈
};

/**
 * Push element x to the back of queue. 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function (x) {
    this.stack1.push(x);
};

/**
 * Removes the element from in front of queue and returns that element.
 * @return {number}
 */
MyQueue.prototype.pop = function () {
    const size = this.stack2.length;
    // 如果输出栈不为空，则直接从出栈弹出数据就可以了。
    if(size) {
        return this.stack2.pop()
    }
    // 输出栈如果为空，就把进栈数据全部导入进来（注意是全部导入）
    while(this.stack1.length) {
        this.stack2.push(this.stack1.pop());
    }
    return this.stack2.pop();
};

/**
 * Get the front element.
 * @return {number}
 */
MyQueue.prototype.peek = function () {
    const x = this.pop();
    this.stack2.push(x); //因为pop函数弹出了元素res，所以再添加回去
    return x;
};

/**
 * Returns whether the queue is empty.
 * @return {boolean}
 */
MyQueue.prototype.empty = function () {
    return !this.stack1.length && !this.stack2.length
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */